package com.putao.album.tag.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.R;
import com.putao.album.base.PtBaseAdapter;
import com.putao.album.tag.bean.TagAlbumPhotoItem;
import com.putao.album.util.BitmapHelper;
import com.putao.album.util.ResourcesHelper;
import com.putao.album.util.StringHelper;

import java.util.List;

/**
 * Created by jidongdong on 15/4/29.
 */
public class TagAlbumPhotoAdapter extends PtBaseAdapter {

    private boolean mDeleteMode = false;

    DisplayImageOptions.Builder optionsBuilder;

    private Bitmap baby_icon_nophoto;

    public TagAlbumPhotoAdapter(Context context, List<TagAlbumPhotoItem> datalist) {
        super(context, datalist);
        optionsBuilder = new DisplayImageOptions.Builder()
                .considerExifParams(true)
                .cacheInMemory(true)
                .cacheOnDisc(true);
        baby_icon_nophoto = ResourcesHelper.getBitmapDrawable(R.drawable.baby_icon_nophoto2).getBitmap();
    }

    /**
     * 设置当前模式为删除模式
     *
     * @param b
     */
    public void setDeleteMode(boolean b) {
        mDeleteMode = b;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_tag_album_photo_item, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.btn_select_icon = (ImageView) convertView.findViewById(R.id.btn_select_icon);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TagAlbumPhotoItem item = (TagAlbumPhotoItem) getItem(position);
        viewHolder.btn_select_icon.setVisibility(View.GONE);
        viewHolder.imageView.setImageBitmap(baby_icon_nophoto);
        if (item.Type == TagAlbumPhotoItem.DataType.NORMAL) {
            if (mDeleteMode && item.canRemove)
                viewHolder.btn_select_icon.setVisibility(View.VISIBLE);
            if (!StringHelper.isEmpty(item.image_url)) {
                optionsBuilder.showImageOnLoading(BitmapHelper.getLoadingDrawable());
                ImageLoader.getInstance().displayImage(item.image_url, viewHolder.imageView, optionsBuilder.build());
            }
            viewHolder.btn_select_icon.setBackgroundResource(item.selected ? R.drawable.album_button_choose : R.drawable.album_button_choose2);
            if (item.is_praise) {//亲一下图标
                viewHolder.btn_select_icon.setVisibility(View.VISIBLE);
                viewHolder.btn_select_icon.setBackgroundResource(R.drawable.album_button_kiss2);
            }
        } else {
            viewHolder.btn_select_icon.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        public ImageView imageView;
        public ImageView btn_select_icon;
    }
}
